package org.dromara.work.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.work.domain.DmNameFile;
import org.dromara.work.domain.DmNameFileCode;
import org.dromara.work.domain.bo.DmNameFileSaveBo;
import org.dromara.work.domain.vo.DmBaseBriefVo;
import org.dromara.work.domain.vo.DmNameFileVo;
import org.dromara.work.mapper.DmNameFileCodeMapper;
import org.dromara.work.mapper.DmNameFileMapper;
import org.dromara.work.service.IDmBaseService;
import org.dromara.work.service.IDmNameFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 备案信息Service业务层处理
 *
 * @author Chen
 * @date 2024-01-16
 */
@RequiredArgsConstructor
@Service
public class DmNameFileServiceImpl implements IDmNameFileService {

    private final DmNameFileMapper baseMapper;

    private final DmNameFileCodeMapper infoCodeMapper;

    private final IDmBaseService dmBaseService;

    /**
     * 查询备案信息
     */
    @Override
    public DmNameFileVo queryById(String id){
        DmNameFileVo entity=baseMapper.selectVoById(id);
        if(entity.getId()!=null){
            List<DmBaseBriefVo>  list=infoCodeMapper.selectDmInfoByID(entity.getId());
            entity.setDmList(list);
        }
        return entity;
    }

    /**
     * 查询备案信息
     */
    @Override
    public TableDataInfo<DmNameFileVo> selectDmFileByCode(String code){
        return TableDataInfo.build(infoCodeMapper.selectDmFileByCode(code));
    }

    /**
     * 查询备案信息列表
     */
    @Override
    public TableDataInfo<DmNameFileVo> queryPageList(DmNameFileSaveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DmNameFile> lqw = buildQueryWrapper(bo);
        Page<DmNameFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        for(int i=0;i<result.getRecords().size();i++){
            List<DmBaseBriefVo> list=new ArrayList<>();
            list=infoCodeMapper.selectDmInfoByID(result.getRecords().get(i).getId());
            result.getRecords().get(i).setDmList(list);
        }
        return TableDataInfo.build(result);




    }

    /**
     * 查询备案信息列表
     */
    @Override
    public List<DmNameFileVo> queryList(DmNameFileSaveBo bo) {
        LambdaQueryWrapper<DmNameFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DmNameFile> buildQueryWrapper(DmNameFileSaveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DmNameFile> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), DmNameFile::getTitle, bo.getTitle());
        return lqw;
    }

    /**
     * 新增备案信息
     */
    @Override
    public Boolean insertByBo(DmNameFileSaveBo bo) {
        DmNameFile add = BeanUtil.toBean(bo, DmNameFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            if(bo.getDmIds().length>0){
                List dms=Convert.toList(bo.getDmIds());
                List<DmNameFileCode> list= new ArrayList<>();
                for (Object dm:dms) {
                    DmNameFileCode info=new DmNameFileCode();
                    info.setBeianId(add.getId());
                    info.setDmCode(dm.toString());
                    list.add(info);
                }
                infoCodeMapper.insertBatch(list);
            }
        }
        return flag;
    }

    /**
     * 修改备案信息
     */
    @Override
    public Boolean updateByBo(DmNameFileSaveBo bo) {
        DmNameFile update = BeanUtil.toBean(bo, DmNameFile.class);
        //validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
                List dms=Convert.toList(bo.getDmIds());
                List<DmNameFileCode> list= new ArrayList<>();
                for (Object dm:dms) {
                    DmNameFileCode info=new DmNameFileCode();
                    info.setBeianId(update.getId());
                    info.setDmCode(dm.toString());
                    list.add(info);
                }
                infoCodeMapper.delete(new LambdaQueryWrapper<DmNameFileCode>().eq(DmNameFileCode::getBeianId,bo.getId()));
                infoCodeMapper.insertBatch(list);
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DmNameFile entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除备案信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


}
